package com.yunhe.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.javabean.Permission;
import com.yunhe.javabean.Role;
import com.yunhe.javabean.Role_Pid;
import com.yunhe.service.PermissionService;
import com.yunhe.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;

    /*
    * 查询所有数据
    * param request page limit
    * */
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page,limit);
        List<Role> all = roleService.findAll();
        PageInfo<Role> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/role/role-list";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String findById(HttpServletRequest request,int id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "/role/role-edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Role role){
        roleService.edit(role);
        return "redirect:/role/findAll";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(){
        return "/role/role-add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Role role){
        roleService.add(role);
        return "redirect:/role/findAll";
    }

    @RequestMapping(value = "/permissionAdd",method = RequestMethod.GET)
    public String toPermissionsAdd(HttpServletRequest request,int id){
        //先查询当前角色拥有的权限
        List<Integer> permissionIdList = roleService.findPermissionByRid(id);
        //将当前角色拥有的权限发送
        request.setAttribute("permissionIdList",permissionIdList);
        //查询所有权限数据
        List<Permission> permissionList = permissionService.findAll();
        //将所有权限发送
        request.setAttribute("permissionList",permissionList);

        request.setAttribute("roleId",id);

        return "/role/role-edit-permission";
    }

    @RequestMapping(value = "/permissionAdd",method = RequestMethod.POST)
    @ResponseBody
    public String toPermissionsAdd(@RequestBody Role_Pid ids){
        roleService.permissionAdd(ids.getRid(),ids.getIds());
        return "/role/findAll";
    }

    @RequestMapping(value = "/delete")
    public String delete(int id){
        roleService.deleteById(id);
        return "redirect:/role/findAll";
    }

    @RequestMapping(value = "/show")
    public String show(HttpServletRequest request,int id){
        Role role = roleService.findRole(id);
        request.setAttribute("role",role);
        return "/role/role-show";
    }






}
